{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Pull Portfolio Performance Data\n",
    "\n",
    "When you create and upload positions to a portfolio in Marquee, GS Quant makes it easy to pull historical performance data with just a few lines of code.\n",
    "\n",
    "## Prerequisites\n",
    "\n",
    "Before you begin this tutorial, you must have a portfolio in Marquee with positions and reports that have already been scheduled and completed. If that's not the case, please navigate to our tutorial on [creating a new portfolio](../tutorials/Create%20New%20Portfolio.ipynb) and/or [updating positions and running reports](../tutorials/Update%20Historical%20Portfolio.ipynb).\n",
    "\n",
    "Your application also needs to have the **run_analytics** scope. If it doesn't, please request it on your [My Applications Page](https://developer.gs.com/go/apps/view). If you have any other questions please reach out to the [Marquee sales team](mailto:gs-marquee-sales@gs.com)."
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Step 1: Authenticate and Initialize Your Session\n",
    "\n",
    "First you will import the necessary modules and add your client id and client secret."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "\n",
    "from gs_quant.markets.portfolio_manager import PortfolioManager\n",
    "from gs_quant.markets.report import  PerformanceReport\n",
    "from gs_quant.session import GsSession, Environment\n",
    "\n",
    "client = 'ENTER CLIENT ID'\n",
    "secret = 'ENTER CLIENT SECRET'\n",
    "\n",
    "GsSession.use(Environment.PROD, client_id=client, client_secret=secret, scopes=('run_analytics',))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Step 2: Get all Portfolio Reports\n",
    "\n",
    "The `PortfolioManager` class allows for easy retrieval and scheduling of portfolio reports. Simply running:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "all_reports = PortfolioManager('ENTER PORTFOLIO ID').get_reports()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "will return a list of `Report` objects that represent the reports associated with the portfolio.\n",
    "\n",
    "## Step 3: Find Portfolio Performance Analytics Report and Pull Data\n",
    "\n",
    "The GS Quant `Report` class is inherited by report subclasses, like `FactorRiskReport` and `PerformanceReport`, each of which corresponds to a type of Marquee report. Each subclass then has additional functions specific to its report type. In this case, we'd like to find the `PerformanceReport` associated with this portfolio, and leverage its functions to retrieve data. In this example, we will pull all historical PnL, gross exposure, and net exposure available.\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "performance_report = list(filter(lambda report: isinstance(report, PerformanceReport), all_reports))[0]\n",
    "pnl = performance_report.get_pnl()\n",
    "gross_exposure = performance_report.get_gross_exposure()\n",
    "net_exposure = performance_report.get_net_exposure()\n",
    "\n",
    "print(f'PnL: \\n{pnl.__str__()}')\n",
    "print(f'Gross Exposure: \\n{gross_exposure.__str__()}')\n",
    "print(f'Net Exposure: \\n{net_exposure.__str__()}')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Quick Tip!\n",
    "If you would like to pull multiple performance measures in one dataframe, you can use the `get_many_measures` function:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "aggregated_performance_results = performance_report.get_many_measures(measures=['pnl', 'grossExposure', 'netExposure'])\n",
    "print(aggregated_performance_results)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "### Quick Tip!\n",
    "If you would only like to pull data for a specific date range, all the functions used above also take in function parameters `start_date` and `end_date`, in which you can pass `datetime.date` objects to specify the range you would like.\n",
    "\n",
    "\n",
    "### You're all set, Congrats! What's next?\n",
    "\n",
    "* [Creating and scheduling a new factor risk report](../examples/marquee/00_create_factor_risk_report.ipynb)\n",
    "\n",
    "* [Updating the portfolio with new positions](../tutorials/Update%20Historical%20Portfolio.ipynb)\n",
    "\n",
    "* [Retrieving the portfolio's performance analytics](../tutorials/Pull%20Portfolio%20Performance%20Data.ipynb)\n",
    "\n",
    "\n",
    "*Other questions? Reach out to the [Portfolio Analytics team](mailto:gs-marquee-analytics-support@gs.com)!*"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}